﻿using Ozone.Help;
using Ozone.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ozone.DAL
{
    public class Sys_UserDAL
    {
        private static List<sys_user> _allsys_users;

        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public static bool AddUserInfo(sys_user Model)
        {
            bool IsSuccess = false;
            try
            {
                SqlSugarScope db = SqlSugarOper.Instance.GetDbClient();
                if (Model != null)
                {
                    IsSuccess = db.Insertable<sys_user>(Model).ExecuteCommand() > 0;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("添加用户信息异常", ex);
                IsSuccess = false;
            }
            LogHelper.Debug("添加用户信息异常");

            return IsSuccess;
        }

        /// <summary>
        /// 删除用户信息 物理删除
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static bool DeleteUserInfo(string ID)
        {
            bool IsSuccess = false;
            try
            {
                SqlSugarScope db = SqlSugarOper.Instance.GetDbClient();
                if (!string.IsNullOrEmpty(ID))
                {
                    IsSuccess = db.Deleteable<sys_user>().Where(p => p.ID == ID).ExecuteCommand() > 0;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("删除日志异常", ex);
                IsSuccess = false;
            }
            return IsSuccess;
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public static bool UpdateUserInfo(sys_user Model)
        {
            bool IsSuccess = false;
            try
            {
                SqlSugarScope db = SqlSugarOper.Instance.GetDbClient();
                if (Model != null)
                {
                    IsSuccess = db.Updateable<sys_user>()
                    .SetColumns(p => p.UserID == Model.UserID)
                    .SetColumns(p => p.Username == Model.Username)
                    .SetColumns(p => p.Password == Model.Password)
                    .SetColumns(p => p.Status == Model.Status)
                    .SetColumns(p => p.RoleId == Model.RoleId)
                    .SetColumns(p => p.CreateTime == Model.CreateTime)
                    .SetColumns(p => p.LastLogin == Model.LastLogin)
                    .Where(p => p.ID == Model.ID)
                    .ExecuteCommand() > 0;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("更新用户信息异常", ex);
                IsSuccess = false;
            }
            LogHelper.Debug("更新用户信息异常");

            return IsSuccess;
        }

        /// <summary>
        /// 查询用户信息
        /// </summary>
        /// <param name="Starttime"></param>
        /// <param name="EndTime"></param>
        /// <param name="FID"></param>
        /// <returns></returns>
        public static List<sys_user> GetUserInfoList(string userID, string password)
        {
            try
            {
                SqlSugarScope db = SqlSugarOper.Instance.GetDbClient();
                if (!string.IsNullOrEmpty(password))
                {
                    //验证用户信息
                    _allsys_users = db.Queryable<sys_user>()
                    .Where(p => p.UserID == userID && p.Password == password && p.Status == 1)
                    .OrderBy(p => p.CreateTime, OrderByType.Desc)
                    .ToList();
                }
                else if (!string.IsNullOrEmpty(userID))
                {
                    //查询对应用户信息
                    _allsys_users = db.Queryable<sys_user>()
                   .Where(p => p.UserID == userID && p.Status == 1)
                   .OrderBy(p => p.CreateTime, OrderByType.Desc)
                   .ToList();
                }
                else
                {
                    //查询所有用户信息
                    _allsys_users = db.Queryable<sys_user>()
                    .Where(p => p.Status == 1)
                   .OrderBy(p => p.CreateTime, OrderByType.Desc)
                   .ToList();
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("查询用户信息发生异常", ex);
                _allsys_users = null;
            }
            return _allsys_users;
        }
    }
}